package lancoo.com.net.retrofitrxjava.okhttp;

import android.text.TextUtils;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import lancoo.com.net.retrofitrxjava.interceptor.RequestEncryptInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by pengmi on 2017/6/14.
 */

public class OkhttpFirst {
//    File httpCacheDirectory = new File(context.getCacheDir(), "responses");
//    //外部存储
//    File httpCacheDirectory = new File(context.getExternalCacheDir(), "responses");
    public static OkHttpClient okHttpClient = new OkHttpClient
            .Builder()
//            .cache(new Cache(new File(getExternalCacheDir(), "responses"),10*1024*1024))
            //有网时使用的拦截器
//           .addNetworkInterceptor(new UrlInterceptor())//调试用
            //所有情况下使用的拦截器
             .addInterceptor(getHttpLoggingInterceptor())
//             .addInterceptor(new RequestHeaderInterceptor())
             .addInterceptor(new RequestEncryptInterceptor())
//             .addInterceptor(new MyRequestHeaderInterceptory())
            .connectTimeout(3000, TimeUnit.MILLISECONDS)
            .readTimeout(3000, TimeUnit.MILLISECONDS)
            .writeTimeout(3000, TimeUnit.MILLISECONDS)
            .build();

    /**
     * 日志输出
     * 自行判定是否添加
     * @return
     */
    static public HttpLoggingInterceptor getHttpLoggingInterceptor(){
        //日志显示级别
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (TextUtils.isEmpty(message)) return;
                Log.d("aaaa","收到响应: " + message);
                if (message.startsWith("{") || message.startsWith("[")) {
//                    LogUtils.d("aaaa","收到响应: " + message);
                }
            }
        });
        loggingInterceptor.setLevel(level);
        return loggingInterceptor;
    }

}
